package com.rede.didiok.rank.restapi;

import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.rede.didiok.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.rede.didiok.common.annotion.log.BussinessLog;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.enums.BehaviorEnum;
import com.rede.didiok.common.enums.SearchTypeEnum;
import com.rede.didiok.common.feign.AdminFeignClient;
import com.rede.didiok.common.feign.CommunityFeignClient;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.StringUtils;
import com.rede.didiok.rank.service.RankMemberService;

@RestController
@RequestMapping("/search/rank")
@Api(value = "SQL搜索相关接口", tags = {"SQL搜索相关接口"})
@Slf4j
public class SearchRestApi {
    @Resource
    private RankMemberService rankMemberService;
    @Resource
    private AdminFeignClient adminFeignClient;
    @Resource
    private CommunityFeignClient communityFeignClient;

    /**
     * 使用SQL语句搜索博客，如需使用Solr或者ElasticSearch 需要启动 mogu-search
     *
     * @param keywords
     * @param currentPage
     * @param pageSize
     * @return
     */
    @AvoidRepeatableCommit(timeout = 500)
    @BussinessLog(value = "搜索rankMember", behavior = BehaviorEnum.RANK_MEMBER_SEARCH)
    @ApiOperation(value = "搜索rankMember", notes = "搜索rankMember")
    @GetMapping("/sqlSearchRankMember")
    public String sqlSearchRankMember(@ApiParam(name = "keywords", value = "关键字", required = true) @RequestParam(required = true) String keywords,
                                @ApiParam(name = "currentPage", value = "当前页数", required = false) @RequestParam(name = "currentPage", required = false, defaultValue = "1") Long currentPage,
                                @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize) {

        if (StringUtils.isEmpty(keywords) || StringUtils.isEmpty(keywords.trim())) {
            return ResultUtil.resultWithMessage(SysConf.ERROR, MessageConf.KEYWORD_IS_NOT_EMPTY);
        }
        if (keywords.trim().length() <= 1 || keywords.trim().length() > 100) {
            return ResultUtil.resultWithMessage(SysConf.ERROR, "搜索长度必须在2~100个字符");
        }

        String searchSize = adminFeignClient.getSysParamsValueByKey(SysConf.SYS_SEARCH_SIZE);
        return ResultUtil.result(SysConf.SUCCESS, rankMemberService.getRankMemberByKeyword(keywords, currentPage, Long.valueOf(searchSize)));
    }

    @AvoidRepeatableCommit(timeout = 500)
    @BussinessLog(value = "根据成员等级获取相关的榜单成员", behavior = BehaviorEnum.RANK_MEMBER_GRADE)
    @ApiOperation(value = "根据成员等级获取相关的榜单成员", notes = "根据成员等级获取相关的榜单成员")
    @GetMapping("/searchRankMemberByGrade")
    public String searchRankMemberByGrade(@ApiParam(name = "gradeUid", value = "成员等级UID", required = true) @RequestParam(name = "gradeUid", required = true) String gradeUid,
                                  @ApiParam(name = "currentPage", value = "当前页数", required = false) @RequestParam(name = "currentPage", required = false, defaultValue = "1") Long currentPage,
                                  @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize) {
        if (StringUtils.isEmpty(gradeUid)) {
            return ResultUtil.resultWithMessage(SysConf.ERROR, "成员等级UID不能为空");
        }
        String searchSize = adminFeignClient.getSysParamsValueByKey(SysConf.SYS_SEARCH_SIZE);
        return ResultUtil.result(SysConf.SUCCESS, rankMemberService.searchRankMemberByGrade(gradeUid, currentPage, Long.valueOf(searchSize)));
    }

    @AvoidRepeatableCommit(timeout = 500)
    @BussinessLog(value = "根据榜单获取相关的成员", behavior = BehaviorEnum.RANK_CATEGORY)
    @ApiOperation(value = "根据榜单获取相关的成员", notes = "根据榜单获取相关的成员")
//    @GetMapping("/searchBlogBySort")
    @GetMapping("/searchRankMemberByRank")
    public String searchRankMemberByRank(@ApiParam(name = "rankUid", value = "榜单UID", required = true) @RequestParam(name = "rankUid", required = true) String rankUid,
                                   @ApiParam(name = "currentPage", value = "当前页数", required = false) @RequestParam(name = "currentPage", required = false, defaultValue = "1") Long currentPage,
                                   @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize) {
        if (StringUtils.isEmpty(rankUid)) {
            return ResultUtil.resultWithMessage(SysConf.ERROR, "榜单uid不能为空");
        }
        String searchSize = adminFeignClient.getSysParamsValueByKey(SysConf.SYS_SEARCH_SIZE);
        return ResultUtil.result(SysConf.SUCCESS, rankMemberService.searchRankMemberByRank(rankUid, currentPage, Long.valueOf(searchSize)));
    }

    @AvoidRepeatableCommit(timeout = 500)
    @BussinessLog(value = "根据创建人获取相关的榜单成员", behavior = BehaviorEnum.RANK_MEMBER_USER_NAME)
    @ApiOperation(value = "根据创建人获取相关的榜单成员", notes = "根据创建人获取相关的榜单成员")
//    @GetMapping("/searchBlogByAuthor")
    @GetMapping("/searchRankMemberByUserName")
    public String searchRankMemberByUserName(@ApiParam(name = "userName", value = "创建人名称", required = true) @RequestParam(name = "userName", required = true) String userName,
                                     @ApiParam(name = "searchType", value = "搜索类型", required = false) @RequestParam(name = "searchType", required = false, defaultValue = "1") String searchType,
                                     @ApiParam(name = "currentPage", value = "当前页数", required = false) @RequestParam(name = "currentPage", required = false, defaultValue = "1") Long currentPage,
                                     @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize) {
        if (StringUtils.isEmpty(userName)) {
            return ResultUtil.resultWithMessage(SysConf.ERROR, "创建人不能为空");
        }
        if (userName.trim().length() > 20) {
            return ResultUtil.resultWithMessage(SysConf.ERROR, "搜索长度必须在1~20个字符");
        }
        String searchSize = adminFeignClient.getSysParamsValueByKey(SysConf.SYS_SEARCH_SIZE);
        if (SearchTypeEnum.RANK_MEMBER.equals(searchType)) {
            return ResultUtil.result(SysConf.SUCCESS, rankMemberService.searchRankMemberByUserName(userName, currentPage, Long.valueOf(searchSize)));
        }
        return ResultUtil.errorWithMessage(MessageConf.PARAM_INCORRECT);
    }

}
